package com.peas.platform.module.icims.base.auth.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.peas.platform.framework.mybatisplus.domain.BaseDomain;
import com.peas.platform.framework.mybatisplus.po.BasePo;
import com.peas.platform.framework.mybatisplus.service.BaseServiceImpl;
import com.peas.platform.module.icims.base.auth.domain.PermissionDomain;
import com.peas.platform.module.icims.base.auth.dto.PermissionDto;
import com.peas.platform.module.icims.base.auth.service.PermissionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class PermissionServiceImpl extends BaseServiceImpl<PermissionDto> implements PermissionService {

    @Resource
    private PermissionDomain permissionDomain;

    @Override
    public BaseDomain<PermissionDto, ? extends BasePo> getDomain() {
        return permissionDomain;
    }

    @Override
    public List<PermissionDto> findListAndParentsByIds(List<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        List<PermissionDto> list = new ArrayList<>();
        for (Long id : ids) {
            PermissionDto per = this.getById(id);
            if (per == null)
                continue;
            list.add(per);
            while (per.getParentId() != null) {
                per = this.getById(per.getParentId());
                if (!list.contains(per)) {
                    list.add(per);
                }
            }
        }
        return list;
    }
}
